package com.tonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tonghe.domain.ResponseResult;
import com.tonghe.domain.dto.ChangeStatus;
import com.tonghe.domain.entity.Recruitment;
import com.tonghe.enums.AppHttpCodeEnum;
import com.tonghe.mapper.RecruitmentMapper;
import com.tonghe.service.RecruitmentService;
import com.tonghe.utils.BeanCopyUtils;
import com.tonghe.vo.PageVo;
import com.tonghe.vo.RecruitmentVo;
import com.tonghe.vo.sys.SysRecruitmentVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Recruitment)表服务实现类
 *
 * @author tonghe
 * @since 2022-08-26 10:31:26
 */
@Service("recruitmentService")
public class RecruitmentServiceImpl extends ServiceImpl<RecruitmentMapper, Recruitment> implements RecruitmentService {

    @Autowired
    private RecruitmentMapper recruitmentMapper;

    @Override
    public ResponseResult getRecruitmentList() {
        // 条件
        LambdaQueryWrapper<Recruitment> queryWrapper = new LambdaQueryWrapper<>();

        // 获取源数据
        List<Recruitment> list = list(queryWrapper);
        List<RecruitmentVo> recruitmentVos = BeanCopyUtils.copyBeanList(list, RecruitmentVo.class);

        // 源数据 流操作以去除过期的数据
        List<RecruitmentVo> collect = recruitmentVos.stream().filter(s -> s.getEndTime().after(new Date())).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult insertRecruitment(Recruitment recruitment) {
        return baseMapper.insert(recruitment)>0 ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult getRecruitment(Integer id) {

        Recruitment recruitment = baseMapper.selectById(id);

        SysRecruitmentVo recruitmentVos = BeanCopyUtils.copyBean(recruitment, SysRecruitmentVo.class);

        return ResponseResult.okResult(recruitmentVos);
    }

    @Override
    public ResponseResult getRecruitmentList(Integer pageSize, Integer pageNum) {

        Page<Recruitment> page = new Page<>();

        page.setSize(pageSize);
        page.setCurrent(pageNum);

        page(page);
        List<Recruitment> records = page.getRecords();
        List<SysRecruitmentVo> recruitmentVos = BeanCopyUtils.copyBeanList(records, SysRecruitmentVo.class);
        // 源数据 流操作以去除过期的数据
//        List<RecruitmentVo> collect = recruitmentVos.stream().filter(s -> s.getEndTime().after(new Date())).collect(Collectors.toList());

        PageVo pageVo = new PageVo(recruitmentVos, (long) recruitmentVos.size());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult updateRecruitment(Recruitment recruitment) {
        int update = baseMapper.updateById(recruitment);
        return update > 0 ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult removeRecruitment(Integer id) {
        int delete = baseMapper.deleteById(id);
        return delete > 0 ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult updateRecruitmentStatus(ChangeStatus changeStatus) {
        int update = recruitmentMapper.updateRecruitmentStatus(changeStatus.getId(), changeStatus.getStatus());
        return update > 0 ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
}
